package com.zuimei.zuimei.fragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.activities.MeiYouHomeActivity;
import com.zuimei.zuimei.adapters.Item5RecycleViewItemAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.FindAppItem5Bean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Jogger on 2017/1/16.
 * 发现应用viewpager内fragment
 */

public class FindAppItem5Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener{
    private List<FindAppItem5Bean> mContentList;
    private List<Object> mList;
    @Bind(R.id.rv_item)
    RecyclerView rv_item;
    @Bind(R.id.srl_refresh)
    SwipeRefreshLayout srl_refresh;
    private Item5RecycleViewItemAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.rv_find_app_item;
    }

    @Override
    public void loadData() {
        srl_refresh.setOnRefreshListener(this);
        new HttpHelper(mActivity).getData(ConstantUtil.FIND_APP_ITEM5_URL, new HttpHelper
                .OnLoadCompletedListener() {


            @Override
            public void onSuccess(String result) {
                mContentList = ParseFindAppJson.getItem5List(result);
                mList = new ArrayList<>();
                rv_item.setLayoutManager(new LinearLayoutManager(mActivity));
                for (int i = 0; i < mContentList.size(); i++) {
                    mList.add(mContentList.get(i).getName());
                    List<FindAppItem5Bean.UserBean> users = mContentList.get(i).getUsers();
                    for (int j = 0; j < users.size(); j++) {
                        mList.add(users.get(j));
                    }
                }
                mAdapter = new Item5RecycleViewItemAdapter(mActivity);
                rv_item.setAdapter(mAdapter);
                mAdapter.loadData(mList);
                mAdapter.setOnItemClickListener(new Item5RecycleViewItemAdapter
                        .OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, FindAppItem5Bean.UserBean userBean) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("user",userBean);
                        startNewActivity(MeiYouHomeActivity.class,"user",userBean);
                        mActivity.overridePendingTransition(R.anim.activity_in_right, R.anim
                                .activity_out_left);
                    }
                });
            }

            @Override
            public void onFailure() {

            }
        });
    }

    @Override
    public void onRefresh() {
        new HttpHelper(mActivity).getRefreshData(ConstantUtil.FIND_APP_ITEM5_URL, new HttpHelper
                .OnLoadCompletedListener() {


            @Override
            public void onSuccess(String result) {
                mContentList = ParseFindAppJson.getItem5List(result);
                mList = new ArrayList<>();
                rv_item.setLayoutManager(new LinearLayoutManager(mActivity));
                for (int i = 0; i < mContentList.size(); i++) {
                    mList.add(mContentList.get(i).getName());
                    List<FindAppItem5Bean.UserBean> users = mContentList.get(i).getUsers();
                    for (int j = 0; j < users.size(); j++) {
                        mList.add(users.get(j));
                    }
                }
                mAdapter.loadData(mList);
                srl_refresh.setRefreshing(false);
            }

            @Override
            public void onFailure() {
                srl_refresh.setRefreshing(false);
            }
        });
    }
}
